////////////////////////////////////////////////
// -- DepthHUD Lite                           //
// by Hurricaaane (Ha3)                       //
//                                            //
// http://www.youtube.com/user/Hurricaaane    //
//--------------------------------------------//
// Main file, with core functions             //
////////////////////////////////////////////////

if not dhlite then
	print("ERROR : Tried to mount DHLITE script without header! Load the shared autorun file instead! (sh_)")
	return
	
end

function dhlite.Initialize( )
	dhlite.MakeCanvas( )
	dhlite_dat._elts = {}
	dhlite_dat._skin_name = "DHLITE_Skin"
	
	// Undebug :: used in think
	/*if self:GetVar("dhlite_core_debug") > 0 then
		local elt = vgui.Create( "DHUI_Health", dhlite_dat._canvas )
		elt:SetSize(64, 32)
		elt:SetMouseInputEnabled( false )
		elt:SetKeyboardInputEnabled( false )
		table.insert( dhlite_dat._elts, elt )
		
	end*/
	
end

function dhlite.__DebugMakeElements()
	
	if not dhlite_dat.__debug_made_elements and (dhlite.GetVar("dhlite_core_debug") > 0) then
		local elt = vgui.Create( "DHUI_Health", dhlite_dat._canvas )
		elt:SetSize(72, 32)
		elt:SetMouseInputEnabled( false )
		elt:SetKeyboardInputEnabled( false )
		table.insert( dhlite_dat._elts, elt )
		
		dhlite_dat.__debug_made_elements = true
	
	elseif dhlite_dat.__debug_made_elements and (dhlite.GetVar("dhlite_core_debug") <= 0) then
		for k,elt in pairs( dhlite_dat._elts ) do
			elt:Remove()
			
		end
		dhlite_dat._elts = {}
		
		dhlite_dat.__debug_made_elements = false
		
	end
	
end

function dhlite.Remove( )
	dhlite.RemoveCanvas( )
	
end

function dhlite.MakeCanvas( )
	if not dhlite_dat._canvas then
		//debug padding
		local debug_padding = 8
		dhlite_dat._canvas = vgui.Create( "DPanel" )
		dhlite_dat._canvas:SetSize( ScrW() - debug_padding * 2, ScrH() - debug_padding * 2 )
		dhlite_dat._canvas:SetPaintBackground( false )
		dhlite_dat._canvas:SetPos( debug_padding, debug_padding )
		dhlite_dat._canvas:SetMouseInputEnabled( false )
		dhlite_dat._canvas:SetKeyboardInputEnabled( false )
		dhlite_dat._canvas:ParentToHUD()
		
	end
	
end

function dhlite.RemoveCanvas( )
	if dhlite_dat._canvas then
		dhlite_dat._canvas:Remove( )
		
	end
	
end

function dhlite.HideCanvas( )
	if dhlite_dat._canvas and dhlite_dat._canvas:IsVisible( ) then
		dhlite_dat._canvas:SetVisible( false )
		
	end
	
end

function dhlite.ShowCanvas( )
	if dhlite_dat._canvas and not dhlite_dat._canvas:IsVisible( ) then
		dhlite_dat._canvas:SetVisible( true )
		
	end
	
end

-- Useful functions for user
function dhlite.StringNiceNameTransform( sInput )
	local sParts = string.Explode("_", sInput)
	local sOutput = ""
	for k,part in pairs(sParts) do
		local len = string.len(part)
		if (len == 1) then
			sOutput = sOutput .. string.upper(part)
		elseif (len > 1) then
			sOutput = sOutput .. string.Left(string.upper(part), 1) .. string.Right(part, len-1)
		end
		if (k ~= #sParts) then sOutput = sOutput .. " " end
	end
	return sOutput
	
end

-- Think Hook
function dhlite.Think()
	dhlite.__DebugMakeElements()
	if dhlite.GetVar("dhlite_core_enable") > 0 then
		dhlite.ShowCanvas( )
		
	else
		dhlite.HideCanvas( )
		
	end
	
end

-- Mount functions
function dhlite.Mount()
	print("")
	print("[ Mounting " .. DHLITE_NAME .. " ... ]")
	
	-- Create all the cvars
	dhlite.CreateVar("dhlite_core_enable", "1", true, false)
	dhlite.CreateVar("dhlite_core_debug", "0", true, false)
	
	-- Make the menu
	if dhlite.MountMenu then
		dhlite.MountMenu()
	end

	-- Create all the concommands
	
	-- Create all the hooks
	hook.Add("Think", "DHLITE_Think", dhlite.Think)
	hook.Add("HUDPaint", "DHLITE_NoticeHUDPaint", dhlite.NoticeHUDPaint)
	
	-- Call all the initialization functions
	dhlite.Initialize( )
	
	print("[ " .. DHLITE_NAME .. " is now mounted. ]")
	print("")
	
end

function dhlite.SetUpMount()
	hook.Add("InitPostEntity", "DHLITE_InitPostEntity", dhlite.Mount)
	
end

function dhlite.Unmount()
	print("")
	print("] Unmounting " .. DHLITE_NAME .. " ... [")
	
	-- Call all the destructor functions
	dhlite.Remove( )
	
	-- Unmount the menu
	if dhlite.UnmountMenu then
		dhlite.UnmountMenu()
	end

	-- Remove all the concommands

	-- Remove all the hooks
	hook.Remove("Think", "DHLITE_Think")
	hook.Remove("Think", "DHLITE_InitPostEntity")
	hook.Remove("HUDPaint", "DHLITE_NoticeHUDPaint")
	
	-- Empty the script-tables
	table.Empty(dhlite_dat)
	table.Empty(dhlite)
	
	dhlite_dat = nil
	dhlite = nil
	
	print("] " .. DHLITE_NAME .. " has been unmounted. [")
	print("")
	
end
